<template>
  <router-view/>
</template>

<script setup>
import {onBeforeMount, provide} from "vue";
import {isMobile} from "src/utils/tools";
import {useFunctionsStore} from "stores/functions";
import {useAPIStore} from "stores/api";
import axios from "axios";
import {useUiStore} from "stores/ui";

const ui = useUiStore();
onBeforeMount(() => {
  getJson();
  whichEquipment();
  initDark();
  loadImg();
});

provide("amrTypes", [
  {
    label: "所有机器人",
    value: null,
  },
  {
    label: "运输机器人（顶升）",
    value: "transport-lift",
  },
  {
    label: "运输机器人（抓取）",
    value: "transport-catch",
  },
  {
    label: "运输机器人（立体库）",
    value: "transport-as-rs",
  },
])

// 获取配置文件更新参数
function getJson() {
  axios.get("./config/config.json?" + Math.random()).then((rs) => {
    if (rs?.data && Object.keys(rs?.data)) {
      try {
        // 把配置文件参数进行设置
        useAPIStore().setIP(rs.data);
        useFunctionsStore().setParams(rs.data);
      } catch (e) {
        // 捕捉异常
        console.log(e);
      }
    }
  });
}

// 进入对应的设备端
function whichEquipment() {
  if (isMobile()) {
    // 是否是移动端
    useFunctionsStore().mode = "server";
  } else if (location.hostname === "localhost") {
    // 是否本地打开
    useFunctionsStore().mode = "device";
  } else {
    if (useFunctionsStore().mode === "remote") return;
    useFunctionsStore().mode = "server";
  }
}

// 初始化主题模式
function initDark() {
  if (ui.isDark) {
    ui.DarkIn();
  } else {
    ui.DarkOut();
  }
}

// 图片预加载
function loadImg() {
  // 加载地图中的点位和机器人
  // 加载姿态中的背景和垃圾桶
  const images = [
    "map/pose.png",
    "map/arrow.png",
    "posture/bg.png",
    "posture/bin.png",
  ];

  for (const img of images) {
    const image = new Image();
    image.src = img;
    image.onload = () => {
    };
  }
}
</script>
